<?php
namespace App\Models\DyModels;
use App\Models\ParentModel;
use DB;
/**
 * 抖音视频model
 * @author puppet
 */
class KeywordVideo extends ParentModel{
  /**
   * 与模型关联的数据表。
   *
   * @var string
   */
  protected $table = 'pt_keyword_video';

  /**
   * DB::直接操作的数据库表名
   * @var string
   */
  protected $db_table = 'pt_keyword_video';

  /**
   * 主键 默认id。
   *
   * @var string
   */
  protected $primaryKey = 'id';

  /**
   * 指定是否模型应该被戳记时间。
   *
   * @var bool
   */
  public $timestamps = false;

  /**
   * 翻页展示所有信息
   */
  public function getPageData($params){
  // echo '<pre/>'; print_r($params);exit();
    $res = false;
    $limit_num = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 5;
    $limit_sta = 0;
    $page = 1;
    $params['start']=0;
    if (isset($params['page']) && intval($params['page']) > 0 &&$params['fenye']!=1) {
      $limit_sta = intval($limit_num * intval($params['page'] - 1));
      $page = intval($params['page']);
    }
    if ($params['fenye']==1){
        $params['limit']=0;

        $limit_sta=0;
        $page = 1;
    }
    $cou_sql = "SELECT count(*) as count FROM {$this->db_table} WHERE 1 = ?";
    $sql = "SELECT id,keyword,cover,title,share_url,is_top,is_reviewed,create_time,digg_count,download_count,forward_count,share_count,comment_count,update_time FROM {$this->db_table} WHERE 1 = ?";
    $values = [1];
    if(isset($params['title']) && !empty($params['title'])){
      $cou_sql .= " AND title like '%{$params['title']}%'";
      $sql .= " AND title like '%{$params['title']}%'";
    }
    if(isset($params['keyword']) && !empty($params['keyword'])){
      $cou_sql .= " AND keyword like '%{$params['keyword']}%'";
      $sql .= " AND keyword like '%{$params['keyword']}%'";
    }

	if(isset($params['start_date']) && !empty($params['start_date'])){
        $params['start_date'] = $params['start_date'] . ' 00:00:00';
		$cou_sql .= " AND update_time >= '{$params['start_date']}'";
		$sql .= " AND update_time >= '{$params['start_date']}'";
	}

	if(isset($params['end_date']) && !empty($params['end_date'])){
        $params['end_date'] = $params['end_date'] . ' 23:59:59';
		$cou_sql .= " AND update_time <= '{$params['end_date']}'";
		$sql .= " AND update_time <= '{$params['end_date']}'";
	}

    $count = DB::select($cou_sql,$values);
     // echo '<pre/>'; print_r($count);exit();
    $count = isset($count[0]['count']) ? $count[0]['count'] : 0;
  //   echo '<pre/>'; print_r($count);exit();
    $sql .= " order by update_time desc LIMIT $limit_sta,$limit_num";
    //  $order_list=array();
    $order_list = DB::select($sql, $values);
	if($order_list){
		$i=0;
		foreach($order_list as $k=>$v){
			$order_list[$k]['kid']=++$i;
		}
	}
    $res['data'] = $order_list;
   //  echo '<pre/>'; print_r($order_list);exit();
    $res['total'] = $count;
    //总条数记录
    return $res;
  }
}